package org.example.usercontroller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.userdto.CustomerLoginDTO;
import org.example.pojo.entity.Customer;
import org.example.pojo.Result;
import org.example.userservice.CustomerService;
import org.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user/login")
public class UserLoginController {

    @Autowired
    private CustomerService customerService;
    @PostMapping()
    public Result login(@RequestBody CustomerLoginDTO customerLoginDTO) {
        Customer customer =customerService.select(customerLoginDTO);
        if(customer == null){
            return Result.error("用户名或密码错误");
        }else{
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", customer.getId());
            claims.put("user", customer.getUser());
            claims.put("email", customer.getEmail());
            String jwt = JwtUtil.generateJwt(claims);
            log.info("登录成功!user:"+customer.getUser());
            return Result.success(jwt);
        }
    }

}
